<template>
	<view class="islogin" v-if="isloginInfo==0">
		<view class="back islogin_container border">
			<view class="islogin_container_again">
				<view class="islogin_name">登录</view>
				<view class="des">
					您暂未登录，是否登录？
				</view>
				<view class="islogin_bottom display space-between ">
					<button v-on:click="onClickMe">取消</button>
					<!-- <button @click="getuserinfo">登录</button> -->
					<button class='testbutton' open-type="getUserInfo" @getuserinfo="getuserinfo" withCredentials="true">登录</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isloginInfo: '',
			}
		},
		created() {
			if (!uni.getStorageSync('openid')) {
				this.isloginInfo = 0
			} else {
				this.isloginInfo = 1
			}
		},
		methods: {
			onClickMe() {
				this.$emit('childClick', '1'); //将this.msg传给父组件
			},
			getuserinfo() {
				let that = this
				uni.showLoading({
					title: '登录中...'
				});
				wx.login({
					success(res) {
						if (res.code) {
							var code = res.code
							var data = {
								code: code
							}
							that.$myRequest.post(that.$api.login.mpLogin, data).then(res1 => {
								if (res1.code == '200') {
									that.getUserListAgain(res1.data.openid)
									
								} else {
									uni.showToast({
										title: res1.msg,
										icon: "none"
									})
								}
							})
						} else {

						}
					},
					fail: err => {
						console.log(err);
					}
				})
			},
			// 再次获取信息
			getUserListAgain(openid) {
				let that = this
				var data = {
					openid: openid
				}
				this.$myRequest.post(this.$api.mine.user, data).then(res => {
					if (res.code == '200') {
						var user = res.data.data
						if (user.edit_state == 0) {
							that.isUpInfo(openid)
						} else {
							uni.hideLoading()
							that.isloginInfo = 1
							uni.setStorageSync('openid',openid)
							// 判断 绑定
							var formopenid = uni.getStorageSync('formopenid')
							if (formopenid) {
								that.bingCode()
							}
							uni.showToast({
								title: '登录成功',
								icon: "success",
								success() {
									that.$emit('childClickAgain');
								}
							})
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			// 判断是否需要上次上传个人信息
			isUpInfo(openid) {
				let that = this
				wx.getUserInfo({
					success: function(res2) {
						uni.hideLoading()
						console.log(res2)
						var userInfo = res2.userInfo
						var data2 = {
							openid: openid,
							nickname: userInfo.nickName,
							avatar: userInfo.avatarUrl,
							gender: userInfo.gender,
							country: userInfo.country,
							province: userInfo.province,
							city: userInfo.city,
						}
						that.isloginInfo = 1
						that.$myRequest.post(that.$api.mine.setUserinfo, data2).then(res3 => {
							if (res3.code == '200') {
								uni.setStorageSync('openid', openid)
								// 判断 绑定
								var formopenid = uni.getStorageSync('formopenid')
								if (formopenid) {
									that.bingCode()
								}
								uni.showToast({
									title: '登录成功',
									icon: "success",
									success() {
										that.$emit('childClickAgain');
									}
								})
							} else {
								uni.showToast({
									title: res3.msg,
									icon: "none"
								})
							}
						})
					},
					fail: res => {
						uni.hideLoading()
						uni.showToast({
							title: '授权失败',
							icon: "none"
						})
						// 获取失败的去引导用户授权 
					}
				})
			},
			// 分享者绑定
			bingCode() {
				let that = this
				var formopenid = uni.getStorageSync('formopenid')
				var data = {
					openid: uni.getStorageSync('openid'),
					ucode: formopenid
				}
				that.$myRequest.post(that.$api.bingCode.bingCode, data).then(res => {
					console.log(res)
					if (res.code == '200') {

					} else {
						console.log(res.msg)
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.islogin {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.6);

		.islogin_container {
			width: 540rpx;
			height: 295rpx;
			border-radius: 8rpx;
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			margin: auto;
			text-align: center;

			.islogin_container_again {
				position: relative;
				height: 295rpx;
			}

			.islogin_name {
				color: #333333;
				font-size: 34rpx;
				padding-top: 45rpx;
			}

			.des {
				color: #555555;
				font-size: 24rpx;
				margin-top: 20rpx;
			}

			.islogin_bottom {
				height: 80rpx;
				line-height: 80rpx;
				margin-top: 65rpx;
				border-top: 1px solid #E6E8EF;
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;
				margin: auto;

				button {
					width: 50%;
					height: 100%;
					font-size: 28rpx;
					color: #294D7C;
					background-color: #fff;
					border: none;
				}

				button::after {
					border: none;
				}

				.left {
					border-right: 1px solid #E6E8EF;
				}
			}
		}

	}
</style>
